import {HeroData} from "../data/HeroData";
import {Config} from "../Config";
import LoadingUI from "../custom/LoadingUI";
import {HINT_TYPE, HintUI} from "../custom/HintUI";
import {RoomList} from "../data/RoomData";
import {ToolUtils} from "../util/ToolUtils";
import {DispatcherUtils} from "../util/DispatcherUtils";
import {Event} from "../Event";
import {network} from "../common/network";
import {cmd} from "../common/cmdClient";
import {ConnectData} from "../data/ConnectData";


export class LoginModule{
    static isReconnecting = false;
    static loginGuest(account:string,password?:string){
        LoadingUI.show("登陆中...");
        if(password == null) password = account;

        network.onOpen(function () {
            network.addHandler(cmd.gate_main_login, LoginModule.svr_gateLoginBack, LoginModule);
            network.sendMsg(cmd.gate_main_login, { "username": account, "password": password });
        }, this);
        network.onClose(function () {
            console.log("gate close");
            HintUI.show("网络异常",HINT_TYPE.ONE)
        }, this);
        network.connect(Config.HOST, Config.PORT);
    }

    static loginWeChatGame(){
        HintUI.show("功能尚未开放",HINT_TYPE.ONE)
    }

    static reconnect(){
        LoadingUI.show("重连中...");
        network.onOpen(LoginModule.svr_onConReconnect, LoginModule);
        network.onClose(LoginModule.svr_onConClose, LoginModule);
        network.connect(ConnectData.host, ConnectData.port);
    }

    static login(){
        if(cc.sys.isBrowser){
            let param = ToolUtils.getUrlParam();
            if(param.account != null){
                LoginModule.loginGuest(param.account,param.password);
            }else{
                LoginModule.loginGuest("yqtest001");
            }
        }else{
            LoginModule.loginGuest("yqtest001");
        }
    }

    static isLogin(){
        //TODO
        //return WebSocketUtils.isConnected()
    }

    static svr_gateLoginBack(msg: { "code": number, "host": string, "port": number, "uid": number, "token": number }) {
        if (msg.code === 0) {
            ConnectData.host = msg.host;
            ConnectData.port = msg.port;
            ConnectData.uid = msg.uid;
            ConnectData.token = msg.token;
            network.onOpen(LoginModule.svr_onConOpen, LoginModule);
            network.onClose(LoginModule.svr_onConClose, LoginModule);
            network.connect(msg.host, msg.port);
        } else if (msg.code === 1) {
            HintUI.show("密码错误",HINT_TYPE.ONE)
            network.disconnect();
        } else {
            HintUI.show("服务器错误",HINT_TYPE.ONE)
            network.disconnect();
        }
    }

    // 网关服连接成功
    static svr_onConOpen() {
        network.addHandler(cmd.connector_main_enter, LoginModule.svr_onEnterBack, LoginModule);
        network.addHandler(cmd.onKicked, LoginModule.svr_onKicked, LoginModule);
        network.sendMsg(cmd.connector_main_enter, { "uid": ConnectData.uid, "token": ConnectData.token });
    }

    // 网管服务重连成功
    static svr_onConReconnect(){
        network.addHandler(cmd.connector_main_reconnectEnter, LoginModule.svr_onEnterBack, LoginModule);
        network.addHandler(cmd.onKicked, LoginModule.svr_onKicked, LoginModule);
        network.sendMsg(cmd.connector_main_reconnectEnter, { "uid": ConnectData.uid, "token": ConnectData.token });
    }

    // 网关服断开连接
    static svr_onConClose() {
        HintUI.show("网络异常，是否重连",HINT_TYPE.TWO,function (flag) {
            if(flag){
                LoginModule.reconnect();
            }
        })
    }

    // 网关服登入回调
    static svr_onEnterBack(msg: { "code": number,"errmsg":string,"role": { "uid": number, "nickname": string, "gold": number, "gameInfo": { "all": number, "win": number },"tableId": number,} }) {
        LoadingUI.exit();
        if (msg.code !== 0) {
            HintUI.show(msg.errmsg,HINT_TYPE.ONE)
            network.disconnect();
            return;
        }
        HeroData.uid = msg.role.uid;
        HeroData.nickname = msg.role.nickname;
        HeroData.gold = msg.role.gold;
        HeroData.gameInfo = msg.role.gameInfo;
        HeroData.tableId = msg.role.tableId;
        DispatcherUtils.dispatchEvent(Event.LOGIN_SUCCESS)
    }

    static svr_onKicked(msg){
        network.removeThisHandlers(LoginModule);
        network.disconnect();
        if(msg.code==1){
            HintUI.show("您的账号已在其他地方登陆。",HINT_TYPE.ONE,function (flag) {
                HeroData.clear();
                DispatcherUtils.dispatchEvent(Event.SERVER_KICKED)
            })
        }
    }
}
